package com.hyyt.shopjsp.statistics.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hyyt.shopjsp.util.common.EnumUtils;
import com.hyyt.shopjsp.util.export.ExportExcel;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.product.vo.ShopProductTypeVo;
import com.hyyt.shopjsp.product.service.IShopProductTypeService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;

/**
 * @author 刘超 on 2017/7/18.
 */

@Controller
@RequestMapping("/back/hotCategoryStatistics")
public class HotCategoryStatisticsController extends BaseController{

    private Logger logger = Logger.getLogger(HotCategoryStatisticsController.class);
    private String basePath = "/back/shop/statistics/";

    @Resource
    private IShopProductTypeService shopProductTypeService;

    /**
     * 跳转热销分类排行统计页面
     *
     * @return 返回到指定页面
     */
    @RequestMapping("gotoHotCategoryStatistics")
    public String gotoHotCategoryStatistics(){
        return basePath + "hotCategoryStatisticsList";
    }

    /**
     * 热销分类排行统计列表
     * @param page
     * @param response
     * @param sortName          商品分类名称
     * @param createTimeStart   开始时间
     * @param createTimeEnd     结束时间
     */
    @RequestMapping(value="listHotCategoryStatistics",method= RequestMethod.POST)
    @ResponseBody
    public void listHotCategoryStatistics(Page page,String createTimeStart,String createTimeEnd,String sortName,HttpServletResponse response){
        Map<String, Object> params=new HashMap<>();
        if(Utils.objectIsNotEmpty(sortName)){
            params.put("sortName", sortName.trim());
        }
        if(Utils.objectIsNotEmpty(createTimeStart)){
            params.put("createTimeStart", createTimeStart);
        }
        if(Utils.objectIsNotEmpty(createTimeEnd)){
            params.put("createTimeEnd", createTimeEnd);
        }
        params.put("sortColumns", "count desc");
        int totalRecordCount =shopProductTypeService.countHotCategoryStatistics(params);
        page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
        List<ShopProductTypeVo> shopProductTypeList=shopProductTypeService.queryHotCategoryStatistics(params, page.getPageRecordBeginIndex(), page.getPageSize());
        params.clear();
        params.put("total", totalRecordCount);// total键 存放总记录数，必须的
        params.put("rows", shopProductTypeList);// rows键 存放每页记录 list
        ResultMsg.create(params, response);  //把内容发送到页面
    }

    /**
     * 导出热销分类排行统计
     * @param createTimeStart
     * @param createTimeEnd
     * @param sortName
     * @param request
     * @param response
     */
    @RequestMapping("exportHotCategoryStatisticsExcel")
    public void exportHotCategoryStatisticsExcel(String createTimeStart, String createTimeEnd, String sortName, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        Map<String, Object> params=new HashMap<>();
        if(Utils.objectIsNotEmpty(sortName)){
            //用于get请求强转，防止中文乱码
            sortName= new String(sortName.trim().getBytes("iso8859-1"),"UTF-8");
            params.put("sortName", sortName);
        }
        if(Utils.objectIsNotEmpty(createTimeStart)){
            params.put("createTimeStart", createTimeStart);
        }
        if(Utils.objectIsNotEmpty(createTimeEnd)){
            params.put("createTimeEnd", createTimeEnd);
        }
        params.put("sortColumns", "count desc");
        int totalRecordCount =shopProductTypeService.countHotCategoryStatistics(params);
        List<ShopProductTypeVo> shopProductTypeList=shopProductTypeService.queryHotCategoryStatistics(params);

        SimpleDateFormat formatter = new SimpleDateFormat(EnumUtils.DataFormat.Y_M_D_H_m_s.getValue());
        List<Map<String,Object>> hotCategory = new ArrayList<>();
        if(shopProductTypeList!=null&&shopProductTypeList.size()>0){
            for (ShopProductTypeVo shopProductTypeVo : shopProductTypeList) {
                Map<String,Object> map = new HashMap<>();
                map.put("sortName", shopProductTypeVo.getSortName());
                map.put("count", shopProductTypeVo.getCount());
                hotCategory.add(map);
            }
        }
        String fileName ="热销分类排行统计";
        String columnNames[] = {"商品分类名称","订单数量（单）"};
        String keys[] = {"sortName","count"};
        try {
            ExportExcel.download(fileName,hotCategory,columnNames,keys,request,response);
        } catch (IOException e) {
            logger.error("导出Excel失败！");
        }
    }


}
